import logging
from dataclasses import dataclass

from src.application.user import dto
from src.application.user.interfaces.reader import GetUsersFilters
from src.application.user.uow import UserUoW
from src.domain.common.constants import Empty, OrderBy

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class GetUsers:
    is_registered: bool | Empty = Empty.UNSET
    offset: int | Empty = Empty.UNSET
    limit: int | Empty = Empty.UNSET
    order: OrderBy = OrderBy.ASC


class GetUsersHandler:
    def __init__(self, *, uow: UserUoW) -> None:
        self._uow = uow

    async def __call__(self, query: GetUsers) -> dto.Users:
        users = await self._uow.user_reader.get_users(
            GetUsersFilters(
                is_registered=query.is_registered,
                offset=query.offset,
                limit=query.limit,
                order=query.order,
            )
        )
        logger.debug(
            "Get users",
            extra={
                "users": users,
                "total": len(users),
                "offset": query.offset,
                "limit": query.limit,
                "is_registered": query.is_registered,
            },
        )
        return dto.Users(
            users=users,
            total=len(users),
            offset=query.offset,
            limit=query.limit,
        )
